<?php 

/*****************************************************************************
 * 
 *  Copyright 2009 Sylvain Dudoit (contact : sylvain.dudoit at gmail.com)
 *  
 *  This file is part of Poupitchi.
 *
 *  Poupitchi is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Poupitchi is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Poupitchi.  If not, see <http://www.gnu.org/licenses/>.
 *
 *****************************************************************************/


///////////////////////////////////////////////////////////////////////////////
// Include db

require_once("db.".P_DB.".php");

///////////////////////////////////////////////////////////////////////////////
// Check form submition

if (isset($_POST['body'])) {
  addMessage($_POST);
}
if (isset($_GET['delete'])) {
  removeMessage($_GET['delete']);
}

///////////////////////////////////////////////////////////////////////////////
// Define list

$start    = isset($_GET['start']) ? $_GET['start'] : 0;
$total    = countMessages();
$nbitems  = P_NB_MESSAGES;

$messages = getMessages($nbitems, $_GET['start']);

///////////////////////////////////////////////////////////////////////////////
// Print page

?>

<?php eLookBloc(); ?>

<div id="bloc_messages">
	<div id="link_form">
		<a href="#bloc_form"> &gt; <?php etr("Add message") ?> &lt; </a>
	</div>
	<?php foreach($messages as $message) : ?>
	  <div class="bloc_message">	    
	    <h2> <?php eFace($message['login']) ?> <?php e($message['title'])?> </h2>
	    <p> <?php e(textToHtml($message['body']))?> </p>
	    <h4> 
	    		<?php etr("Posted at")?> : <?php e(date(P_DATE, $message['date']))?> 
	    		<?php if ($P_ADMIN) : ?>
	    		<a href="index.php?action=show&module=messages&delete=<?php e($message['id']); ?>"
	    			 onclick="return confirm('<?php etr("Are you sure you want to delete ?"); ?>')">
	    				[ <?php etr("Delete"); ?> ] 
	    		</a>
	    		<?php endif; ?>
	    </h4>
	  </div>
	<?php endforeach;?>
	<?php ePagination($start, $total, $nbitems); ?>	
	<div id="bloc_form">
		<h2> &gt; <?php etr("Add message") ?> &lt; </h2>
	  <form action="index.php?action=show&module=messages" method="post">
	    <table>
	      <tr>
	        <th><?php etr("Title")?></th>
	        <td><input type="text" class="title" name="title" size="50" value="" /></td>
	      </tr>
	      <tr>
	        <th><?php etr("Body")?></th>
	        <td><textarea name="body" class="body" cols="50" rows="20"></textarea></td>
	      </tr>
	      <tr>
	        <th>&nbsp;</th>
	        <td><input type="submit" class="submit" value="<?php etr("Save")?>"/></td>
	      </tr>
	    </table>
	  </form>
	</div>
</div>
